package com.tuanzi.loan.web.bean.miniApps.recommend;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.service.miniApps.recommend.HealthHeadlineRecommendService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.miniApps.recommend.HealthHeadlineRecommendRequest;
import com.tuanzi.loan.vo.response.miniApps.recommend.HealthHeadlineRecommendResponse;
import com.tuanzi.loan.vo.view.miniApps.MiniAppsRelationVo;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.miniApps.recommend.HealthHeadlineRecommendListLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.bean.ViewScoped;
import java.text.SimpleDateFormat;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.ONLINE_OFFLINE_STATUS;

/**
 * 养生头条推荐位列表
 * @author weibifeng 
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class HealthHeadlineRecommendListBean extends JSFListingSessionBean<HealthHeadlineRecommendRequest, HealthHeadlineRecommendResponse> {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
    @Autowired
    HealthHeadlineRecommendService healthHeadlineRecommendService;
    
    private List<MiniAppsRelationVo> miniAppsList = null; // 小程序列表
    private List<MiniAppsRelationVo> advertiseList = null; // 广告位列表
    
    public String onAdd() {
		return redirect("/views/miniApps/recommend/health-headline-recommend-info.xhtml");
	}
    
    public String onEdit(String id) {
		return redirect("/views/miniApps/recommend/health-headline-recommend-info.xhtml?articleRelationId=" + id);
	}

    private void initList(){
    	miniAppsList = healthHeadlineRecommendService.getMiniAppsList();
    	advertiseList = healthHeadlineRecommendService.getAdvertiseList();
    }
    
    public void onDeleteAdvertise(Long articleRelationId) {
    	healthHeadlineRecommendService.deleteAdvertise(articleRelationId);
    	addSuccessfullyMessage();
    }
    
    /**
     * 广告位上线
     * @param id
     */
    public void onAdvertiseOnline(Long articleRelationId) {
    	HealthHeadlineRecommendRequest request = new HealthHeadlineRecommendRequest();
    	request.setArticleRelationId(articleRelationId);
    	request.setOperator($.getOperator());
    	healthHeadlineRecommendService.updateAdvertiseStatus(request, 1);
		addSuccessfullyMessage();
	}
    
    /**
     * 广告位下线
     * @param id
     */
    public void onAdvertiseOffline(Long articleRelationId) {
    	HealthHeadlineRecommendRequest request = new HealthHeadlineRecommendRequest();
    	request.setArticleRelationId(articleRelationId);
    	request.setOperator($.getOperator());
    	healthHeadlineRecommendService.updateAdvertiseStatus(request, 0);
		addSuccessfullyMessage();
    }
    
    public void onFlushCache(){
    	healthHeadlineRecommendService.cleanCache();
        initList();
        addInfoMessage("缓存更新成功!");
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new HealthHeadlineRecommendListLazyDataModel($, session, healthHeadlineRecommendService);
        addSuccessfullyMessage();
    }

    @Override
    protected String page() {
        return "/views/miniApps/recommend/health-headline-recommend-list.xhtml";
    }

    @Override
    protected void initLazyDataModel() {
        $ = new HealthHeadlineRecommendRequest();
        lazyDataModel = new HealthHeadlineRecommendListLazyDataModel($, session, healthHeadlineRecommendService);
        initList();
    }
    
    @Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(ONLINE_OFFLINE_STATUS);
	}
    
}




